// 日期区间
laydate.render({
    elem: '#between'
    ,range: true
  });
// 车辆选择
$('.number').on('click',()=>{
    opsearch('{:url("html/car/car")}?op=search')
 })

var page = 1;
var pagenum = 10;
var fi=1;
var url={
    list:api.AS.list,
    delete:api.AS.delete,
}
var data={};
data.page=page;
data.pagenum=pagenum;
var where_json={};


{if $Think.get.filter}
    data.with_trashed =2;
    var main=[[ //表头
    
       {field: 'id', title: '年检编号', fixed: 'left'}
       ,{field: 'number', title: '车牌号',templet: '#carId',width:110}
       ,{field:'bill_no', title:'单据编号'}
       ,{field: 'start_date', title: '年检日期',width:120}  
       ,{field: 'end_date', title: '过期时间',width:120}  
       ,{field: 'service', title: '年检地点',width:180,templet:'<div>{{d.service.servive_name?d.service.servive_name:"未填写"}}</div>'}
       ,{field:'checker', title:'年检人',width:120}
       ,{field:'sender', title:'送检人',width:120}
       ,{field: "state", title:'年检状态',templet:'#state'}
       ,{field:'price', title:'年检费用'}  
    //    ,{field:'remark', title:'备注'}  
      
   ]];
{else/}
var main=[[ //表头
    {type:'checkbox', fixed: 'left'}
   ,{field: 'id', title: '年检编号', fixed: 'left'}
   ,{field: 'number', title: '车牌号',templet: '#carId',width:110}
   ,{field:'bill_no', title:'单据编号'}
   ,{field: 'start_date', title: '年检日期',width:120}  
   ,{field: 'end_date', title: '过期时间',width:120}  
   ,{field: 'service', title: '年检地点',width:180,templet:'<div>{{d.service.service_name?d.service.service_name:"未填写"}}</div>'}
   ,{field:'checker', title:'年检人',width:120}
   ,{field:'sender', title:'送检人',width:120}
   ,{field: "state", title:'年检状态',templet:'#state'}
   ,{field:'price', title:'年检费用'}  
   // ,{field:'remark', title:'备注'}  
   ,{ title: '操作',width:120, toolbar:'#demo',fixed: 'right'}
]];
{/if}



{if $Think.get.car_id}
where_json['car.car_id']=['in','{$Think.get.car_id}']
{/if}

{if $Think.get.between}
    var start = '{$Think.get.between}'.split(' ');
    where_json.start_date=['between time',[start[0],start[2]]]
{/if}



data.where_json=JSON.stringify(where_json)
console.log(data)
ajaxAS(data);
function ajaxAS(data) {
    Load();
    //请求数据
    ajax(url.list, data, 'post').done(function (res) {
        Load(1);
        // 成功回调
       // 添加年检记录跳转
        $('#add').on('click',()=>{
            window.location.href = path.AS.add
        })
        $('#delete').on('click',()=>{
            window.location.href = path.AS.index+'?filter=delete'
        })
        $('#use').on('click',()=>{
            window.location.href = path.AS.index
        })
        //方法级表单渲染
        console.log(res)
        table.render({
            elem: '#AS',
            cols: main,
            data: res.data,
            limit: pagenum,
            cellMinWidth: 80
        });
        if (fi == 1) {
            laypage.render({
                elem: 'page',
                count: res.count,
                layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'],
                theme: '#1E9FFF',
                jump: function (obj, first) {
                    console.log(obj)
                    if (!first) {
                        page = obj.curr;
                        data.page = page;
                        pagenum = obj.limit;
                        data.pagenum = pagenum;
                        ajaxAS(data);
                    } else {

                    }
                }
            })
            fi = 2;
        }
        // 操作
        table.on('tool(demo)', function (obj) {
                    var data1 = obj.data; //获得当前行数据
                    var data2 = {
                        ids:data1.id
                    };
                    var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
                    var tr = obj.tr; //获得当前行 tr 的DOM对象
                    if (layEvent === 'edit') {
                        console.log(data1)
                        console.log('编辑')
                        window.location.href = "{:url('/html/plus/annual_inspection_add')}?id="+data1.id;
                    } else if (layEvent === 'del') {
                        layer.confirm('是否删除', function (index) {
                                Load()
                                ajax(url.delete, data2, 'post').done(function (res) {
                                    // 成功回调
                                    console.log(res)
                                    layer.closeAll();
                                    Msg('删除成功');
                                    ajaxAS(data)
                                }).fail(function (err) {
                                    // 失败回调
                                    layer.close(deleteload);
                                    Err(res.msg ? res.msg : '操作失败，稍候再试');
                                });
                                layer.close(index);
                            })
                        }
                    })

            		//删除多个
    			$(".delete-multiple").on('click', function () {
    				var ids = '';
                    var checkStatus = table.checkStatus('AS'); //test即为基础参数id对应的值
                    console.log('多选信息')
                    console.log(checkStatus)
    				console.log(checkStatus.data) //获取选中行的数据
    				console.log(checkStatus.data.length) //获取选中行数量，可作为是否有选中行的条件
                    console.log(checkStatus.isAll) //表格是否全选
                    if(checkStatus.data.length){
    				for (var dei = 0; dei < checkStatus.data.length; dei++) {
    					if (dei == 0) {
    						ids = checkStatus.data[dei].id
    					} else {
    						ids += ("," + checkStatus.data[dei].id)
    					}
                    }
                    var data2 ={
                        ids : ids
                    }
    				layer.open({
    					type: 1,
    					title: false,
    					id: "del",
    					content: '<div style="padding: 20px 100px;">是否删除</div>',
    					btn: ['确定', '取消'],
    					btnAlign: 'c' //按钮居中
    						,
    					shade: 0 //不显示遮罩
    						,
    					yes: function () {
    						var deleteload = layer.load(1, {
    							shade: [0.1, '#fff'] //0.1透明度的白色背景
                            });
                            console.log(data2)
                            ajax(url.delete,data2,'post').done(function(resp) {
                                // 成功回调
                            			layer.close(deleteload);
                                        layer.closeAll();
                                        Msg('删除成功');
                                        ajaxAS(data)
                                }).fail(function(err) {
                                // 失败回调
                                layer.close(deleteload);
    						    Err(res.msg ? res.msg : '操作失败，稍候再试');
                                });
    					},
    					btn2: function () {
    						layer.closeAll();
    					}
                    });
                }else{
                    Err('未有选择项')
                }
    			});
                if(res.code ==1){

                }else{
                    Err(res.msg)
                }
    }).fail(function (err) {
        // 失败回调
        Load(1)
        Err('请求失败')
    });
};
